package com.web.a.weba.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

@Activate(group = {Constants.CONSUMER},order = -1000)
public class TransportIpFilter implements Filter {

    ThreadLocal<String> threadLocalIp =new ThreadLocal<>();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {


        String ip = (String) RpcContext.getContext().getAttachment("ip");
        if (null!=ip){
            threadLocalIp.set(ip);
        }else {
            ip=threadLocalIp.get();
        }
        RpcContext.getContext().setAttachment("ip", ip);
         Result invoke = invoker.invoke(invocation);
        return invoke;
    }
}